<?php
	require_once( 'core_DataBaseConnect.php' );
	require_once( '_configs.php' );
	
	class FreeBoardDAO
	{
		// 새 모델 추가
		public static function insertBoard( DataBaseConnect $con, 
											$ownerID, 
											$title, 
											$desc )
		{
			$query = 'INSERT INTO '.configs::TABLE_FREEBOARD.
						"(
							`TITLE` ,
							`DESC` ,
							`REGIST_DATE`,
							`OWNER_ID`
						)
						VALUES 
						(
							'$title',
							'$desc',
							NOW(),
							'$ownerID'
						);";
			return $con->query( $query );
		}
		
		// 추가 썸네일 경로 갱신
		public static function insertBoardThumbs( DataBaseConnect $con, 
													$board_seq, 
													$thumbnails )
		{
			foreach( $thumbnails as $k=>$v )
			{
				$query = 'INSERT INTO '.configs::TABLE_FREEBOARDTHUMBS.
							"(
								`SEQ` ,
								`B_SEQ` ,
								`LINK`
							)
							VALUES 
							(
								NULL,
								'$board_seq',
								'$v'
							);";
				$con->query( $query );
			}
		}
		
		// 전체 ROW 수
		public static function countListItems( DataBaseConnect $con )
		{
			// 모든 모델 정보를 대상으로 한다.
			$query 	= 'SELECT COUNT(*) FROM '.configs::TABLE_FREEBOARD.';';
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(*)' ];
		}
		
		// 모든 목록 가져오기
		public static function selectFreeBoardList( DataBaseConnect $con,
															$page,
															$numPerPage,
															$isAdmin )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			// 모든 모델 정보를 대상으로 한다.
			$query = 'SELECT t1.*, t2.NICKNAME AS OWNER_NICKNAME 
						FROM '.configs::TABLE_FREEBOARD.' AS t1 
						JOIN '.configs::TABLE_MEMBER.' AS t2 
						ON 
							t1.OWNER_ID = t2.SEQ ';
			// 관리자용이 아니라면, 비활성 게시글은 제외한다.
			if( $isAdmin == false )
			{
				$query	.= 'WHERE t1.STATUS=\''.configs::STATUS_ACTIVE.'\' ';
			}
						
			// 
			$query .="	ORDER BY 
							t1.REGIST_DATE DESC
						LIMIT 
							$startIndex,$numPerPage;";
			return $con->query( $query );
		}
		
		
		// 모든 목록 가져오기
		public static function selectFreeBoardDetail( DataBaseConnect $con, $seq )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			// 모든 모델 정보를 대상으로 한다.
			$query = 'SELECT t1.*, t2.NICKNAME AS OWNER_NICKNAME 
						FROM '.configs::TABLE_FREEBOARD.' AS t1 
						JOIN '.configs::TABLE_MEMBER.' AS t2 
						ON t1.OWNER_ID = t2.SEQ '."
						WHERE t1.SEQ = $seq 
						LIMIT 1;";
			return $con->query( $query );
		}
		
		// 모델에 설정된 모든 썸네일 정보를 가져옴
		public static function selectThumnails( DataBaseConnect $con, $board_seq )
		{
			$query = 'SELECT * FROM '.configs::TABLE_FREEBOARDTHUMBS." WHERE `B_SEQ`='$board_seq';";
			return $con->query( $query );
		}
		
		// 멤버가 모델의 댓글을 등록
		public static function insertFreeBoardComment( DataBaseConnect $con, 
													$login_member_id, 
													$boardSeq,
													$comment )
		{
			$query = 'INSERT INTO '.configs::TABLE_FREEBOARDCOMMENTS.
						"(
							`B_SEQ`,
							`OWNER_ID`,
							`COMMENT`,
							`REGISTER`
						)VALUES(
							'$boardSeq',
							'$login_member_id',
							'$comment',
							NOW()
						);";
			return $con->query( $query );
		}
		
		// 모든 댓글 수 가져오기
		public static function countFreeBoardComments( DataBaseConnect $con,
														$boardSeq )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_FREEBOARDCOMMENTS." 
						WHERE `B_SEQ`='$boardSeq';";
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ];
		}
		
		// 모든 댓글 목록 가져오기
		public static function selectFreeBoardComments( DataBaseConnect $con,
												$boardSeq,
												$page,
												$numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT t1.*, t2.NICKNAME 
							FROM '.configs::TABLE_FREEBOARDCOMMENTS.' AS t1 
							JOIN '.configs::TABLE_MEMBER." AS t2 
							ON t1.OWNER_ID = t2.SEQ
							WHERE 
								`B_SEQ`='$boardSeq' 
							LIMIT 
								$startIndex,$numPerPage;";
								
			return $con->query( $query );
		}
		
		
		// 댓글 삭제하기
		public static function deleteFreeBoardComment( DataBaseConnect $con,
												$seq,
												$login_member_id)
		{
			$query = 'DELETE FROM '.configs::TABLE_FREEBOARDCOMMENTS." WHERE `SEQ`='$seq' AND `OWNER_ID`='$login_member_id' LIMIT 1;";
			$con->query( $query );
			return ( $con->affected_rows() > 0 );
		}
		
		// 특정 코멘트를 조회함
		public static function selectFreeBoardCommentBySeq( DataBaseConnect $con, $seq )
		{
			$query = 'SELECT * FROM '.configs::TABLE_FREEBOARDCOMMENTS." WHERE `SEQ`='$seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		
		// 게시글 소유자 확인
		public static function isOwner( DataBaseConnect $con, $seq, $login_member_id )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_FREEBOARD." 
						WHERE 
							`SEQ`='$seq'
							AND
							`OWNER_ID`='$login_member_id'
						LIMIT
							1;";
			$result = $con->query( $query );
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			return $fetch[ 'COUNT(SEQ)' ] == '1';
		}
		
		
		
		
		
		
		// 썸네일 이미지 경로
		public static function selectThumbPath( DataBaseConnect $con,
														$thumb_seq )
		{
			$query = 'SELECT LINK FROM '.configs::TABLE_FREEBOARDTHUMBS." WHERE `SEQ`='$thumb_seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		// 썸네일 정보를 삭제
		public static function deleteThumbPath( DataBaseConnect $con,
														$thumb_seq )
		{
			$query = 'DELETE FROM '.configs::TABLE_FREEBOARDTHUMBS." WHERE `SEQ`='$thumb_seq' LIMIT 1;";
			return $con->query( $query );
		}
		
		
		// 정보 업데이트
		public static function updateFreeBoard(	DataBaseConnect $con,
												$seq,
												$title, 
												$desc )
		{
			$query = 'UPDATE '.configs::TABLE_FREEBOARD."
			 			SET
							`TITLE`='$title',
							`DESC`='$desc'
						WHERE
							`SEQ`='$seq'
						LIMIT 1;";
			return $con->query( $query );
		}
		
		// 상태 업데이트
		public static function updateFreeBoardStatus( DataBaseConnect $con, $seq, $status )
		{
			$query = 'UPDATE '.configs::TABLE_FREEBOARD."
			 			SET
							`STATUS`='$status'
						WHERE
							`SEQ`='$seq'
						LIMIT 1;";
			return $con->query( $query );
		}
	}
?>